package cn.edu.scau.cmi.ema.controller;

import cn.edu.scau.cmi.ema.base.CmiPagination;
import cn.edu.scau.cmi.ema.base.CmiEmaController;
import cn.edu.scau.cmi.ema.domain.Food;
import cn.edu.scau.cmi.ema.domain.Foodmaterial;
import cn.edu.scau.cmi.ema.domain.Material;
import cn.edu.scau.cmi.ema.util.CmiHibernateProxyUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Set;

@Controller("FoodmaterialController")
public class FoodmaterialController extends CmiEmaController {

	@RequestMapping("/listFoodmaterials")
	public ModelAndView listFoodmaterials(HttpServletRequest request, @ModelAttribute CmiPagination pagination) {
		ModelAndView mav = new ModelAndView();

		pagination = foodmaterialPaginationService.updatePagination(request, pagination);
		Set<Foodmaterial> foodmaterials = foodmaterialPaginationService.getPaginationFoodmaterials(pagination);

		mav.addObject("foodmaterials", foodmaterials);
		mav.addObject("entityName", "foodmaterial");
		mav.addObject("pagination", pagination);
		mav.setViewName("foodmaterial/listFoodmaterials");
		return mav;
	}

	public String listFoodmaterials() {
		return "redirect:/listFoodmaterials";
	}

	@RequestMapping("/viewFoodmaterial")
	public ModelAndView viewFoodmaterial(@RequestParam Integer foodmaterialId) {
		ModelAndView mav = new ModelAndView();
		mav.addObject("foodmaterial", foodmaterialDAO.findFoodmaterialByPrimaryKey(foodmaterialId));
		mav.setViewName("foodmaterial/viewFoodmaterial");
		return mav;
	}

	@RequestMapping(value = "/confirmDeleteFoodmaterials")
	public ModelAndView confirmDeleteFoodmaterials(
			@RequestParam(value = "deleteFoodmaterialsIds", required = true) Set<Integer> deleteFoodmaterialsIds) {
		ModelAndView mav = new ModelAndView();

		Set<Foodmaterial> deleteFoodmaterials = foodmaterialDAO.findFoodmaterialsByIds(deleteFoodmaterialsIds);
		Set<Foodmaterial> referencedFoodmaterials = foodmaterialService.loadReferenceFoodmaterials(deleteFoodmaterials);
		boolean hasReferenced;
		if (referencedFoodmaterials.size() == 0) {
			mav.addObject("deleteEntitys", deleteFoodmaterials);
			hasReferenced = false;
		} else {
			deleteFoodmaterials.removeAll(referencedFoodmaterials);
			mav.addObject("referencedEntitys", referencedFoodmaterials);
			mav.addObject("deleteEntitys", deleteFoodmaterials);
			hasReferenced = true;
		}
		mav.addObject("entityName", "Foodmaterial");
		mav.addObject("hasReferenced", hasReferenced);
		mav.setViewName("forward:/listFoodmaterials");
		return mav;
	}

	@RequestMapping("/deleteFoodmaterial")
	public String deleteFoodmaterial(@RequestParam Integer foodmaterialId) {
		Foodmaterial foodmaterial = foodmaterialDAO.findFoodmaterialByPrimaryKey(foodmaterialId);
		foodmaterialService.deleteFoodmaterial(foodmaterial);
		return "forward:/listFoodmaterials";
	}

	@RequestMapping("/confirmDeleteFoodmaterial")
	public ModelAndView confirmDeleteFoodmaterial(@RequestParam Integer foodmaterialId) {

		ModelAndView mav = new ModelAndView();

		mav.addObject("foodmaterial", foodmaterialDAO.findFoodmaterialByPrimaryKey(foodmaterialId));

		mav.setViewName("foodmaterial/deleteFoodmaterial");

		return mav;
	}

	@RequestMapping("/deleteFoodmaterials")
	public String deleteFoodmaterials(
			@RequestParam(value = "deleteFoodmaterialsId", required = true) Set<Integer> deleteFoodmaterialsId) {
		foodmaterialService.deleteFoodmaterials(deleteFoodmaterialsId);
		return "forward:/listFoodmaterials";
	}

	@RequestMapping("/editFoodmaterial")
	public ModelAndView editFoodmaterial(@RequestParam Integer foodmaterialId) {

		ModelAndView mav = new ModelAndView();

		Foodmaterial foodmaterial = foodmaterialDAO.findFoodmaterialByPrimaryKey(foodmaterialId);
		// 将需要编辑的对象加到mav对象中
		mav.addObject("foodmaterial", foodmaterial);

		// 把所有的引用foodmaterial对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(foodmaterialService.loadFoodmaterialObjectReferencedSet(foodmaterial));

		// 将编辑页面foodmaterial添加到mav中
		mav.setViewName("foodmaterial/editFoodmaterial");
		return mav;
	}

	@RequestMapping("/newFoodmaterial")
	public ModelAndView newFoodmaterial() {
		ModelAndView mav = new ModelAndView();
		Foodmaterial foodmaterial = new Foodmaterial();

		mav.addObject("foodmaterial", foodmaterial);

		// 把所有的引用foodmaterial对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(foodmaterialService.loadFoodmaterialObjectReferencedSet(foodmaterial));

		mav.addObject("newFlag", true);
		mav.setViewName("foodmaterial/editFoodmaterial");

		return mav;
	}

	@RequestMapping("/saveFoodmaterial")
	public ModelAndView saveFoodmaterial(@ModelAttribute Foodmaterial foodmaterial) {

		ModelAndView mav = new ModelAndView();

		Food foodTemp = foodmaterial.getFood();
		Food food = foodTemp == null ? null : CmiHibernateProxyUtil.deproxy(foodDAO.findFoodById(foodTemp.getId()));
		Material materialTemp = foodmaterial.getMaterial();
		Material material = materialTemp == null ? null
				: CmiHibernateProxyUtil.deproxy(materialDAO.findMaterialById(materialTemp.getId()));

		Foodmaterial existingFoodmaterial = CmiHibernateProxyUtil.deproxy(foodmaterialDAO
				.findFoodmaterialByPhysicalPrimaryKey(food.getId(), material.getId(), foodmaterial.getContent()));

		foodmaterial.setFood(food);
		foodmaterial.setMaterial(material);
		// 新建记录
		if (foodmaterial.getId() == null) {
			if (foodmaterial.equals(existingFoodmaterial)) {

				mav.addAllObjects(foodmaterialService.loadFoodmaterialClassReferencedSet(foodmaterial));
				// mav = editContinue(mav, foodmaterial);
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("foodmaterial/editFoodmaterial");
			} else {
				foodmaterialService.saveFoodmaterial(foodmaterial);
				mav.setViewName("forward:/listFoodmaterials");
			}
		}
		// 编辑记录
		else {
			Foodmaterial originFoodmaterial = CmiHibernateProxyUtil
					.deproxy(foodmaterialDAO.findFoodmaterialByPrimaryKey(foodmaterial.getId()));
			if (!foodmaterial.equals(originFoodmaterial) && foodmaterial.equals(existingFoodmaterial)) {
				mav.addAllObjects(foodmaterialService.loadFoodmaterialClassReferencedSet(foodmaterial));
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("foodmaterial/editFoodmaterial");
			} else {
				foodmaterialService.saveFoodmaterial(foodmaterial);
				mav.setViewName("forward:/listFoodmaterials");
			}
		}
		return mav;
	}

	@RequestMapping("/foodmaterialController/binary.action")
	public ModelAndView streamBinary(@ModelAttribute HttpServletRequest request,
			@ModelAttribute HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("streamedBinaryContentView");
		return mav;

	}

}
